package com.chinacache.maint.webssh.service.ssh;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.transaction.annotation.Transactional;

import com.chinacache.maint.commons.orm.hibernate.Page;
import com.chinacache.maint.commons.orm.hibernate.SimpleHibernateDao;
import com.chinacache.maint.webssh.entity.ssh.CommandLog;

@Transactional
public class CommandLogManager {
	private SimpleHibernateDao<CommandLog, Long> commandLogDao;

	@Required
	public void setSessionFactory(SessionFactory sessionFactory) {
		commandLogDao = new SimpleHibernateDao<CommandLog, Long>(
				sessionFactory, CommandLog.class);
	}

	@Transactional(readOnly = true)
	public CommandLog getCommandLog(Long id) {
		return commandLogDao.findById(id);
	}

	@Transactional(readOnly = true)
	public Page<CommandLog> getAllCommandLog(Page<CommandLog> page) {
		return commandLogDao.findAll(page);
	}

	public void saveCommandLog(CommandLog commandLog) {
		commandLogDao.save(commandLog);
	}

	@Transactional(readOnly = true)
	public Page<CommandLog> getAllCommandLogByCondition(Page<CommandLog> page,
			String condition) {
		if (condition != null && condition.length() > 0) {
			condition = condition.trim();
			String queryString = "select commandLog from CommandLog commandLog where commandLog.commandLog like "
					+ "'%"
					+ condition
					+ "%'"
					+ " or commandLog.time like "
					+ "'%"
					+ condition
					+ "%'"
					+ " or commandLog.device.devId like "
					+ "'%"
					+ condition
					+ "%'"
					+ " or commandLog.userName like "
					+ "'%" + condition + "%' order by commandLog.id";
			return commandLogDao.find(page, queryString);        
		} else {
			return commandLogDao.findByCriteria(page,"id",false);
		}
	}
}
